package com.hankol.newretail.respository

import androidx.lifecycle.MutableLiveData
import com.baselibrary.base.BasicListBean
import com.baselibrary.base.BasicBean
import com.baselibrary.bean.ReceiveAddressBean
import com.baselibrary.bean.WxPaySignBean
import com.baselibrary.extension.checkNotNull
import com.baselibrary.net.APIResponse
import com.baselibrary.net.MyHttpUtil
import com.baselibrary.net.result.HttpResult
import com.baselibrary.utils.GsonUtils
import com.baselibrary.utils.SpUtils
import com.hankol.newretail.api.NewRetailApi
import com.hankol.newretail.bean.*
import com.hankol.newretail.bean.goods_details.GoodsDetails
import com.hankol.newretail.bean.goods_details.GoodsGiftBean
import com.hankol.newretail.bean.goods_details.GrassShowBean
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

/**新零售
 * @author: merbng
 * @time: 2022/8/4 11:34
 * @description:
 */
class NewReatilRequestManager {
    companion object {
        @Volatile
        private var instance: NewReatilRequestManager? = null

        fun getInstance(): NewReatilRequestManager {
            return instance ?: synchronized(this) {
                instance ?: NewReatilRequestManager().also { instance = it }
            }
        }
    }

    /**
     *  获取商城首页相关数据
     */
    fun requestHomeMallInfo(liveData: MutableLiveData<HomeMallInfoBean>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestHomeMallInfo().subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : APIResponse<HomeMallInfoBean>(this) {
            override fun onSuccess(t: HomeMallInfoBean, json: String) {
                if (t.checkNotNull()) liveData.value = t
            }

            override fun onError(e: Throwable) {
                liveData.value = null
                super.onError(e)
            }
        })
    }

    /**
     *  商城首页-分类
     */
    fun requestCategoryList(liveData: MutableLiveData<List<MallTabBean>>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestCategoryList().subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<List<MallTabBean>>(this) {
            override fun onSuccess(t: List<MallTabBean>, json: String) {
                liveData.value = t
            }

            override fun onError(e: Throwable) {
                liveData.value = null
            }
        })
    }

    /**
     * 获取商城分类 商品列表
     */
    fun requestGoodClassifyList(type: String, page: Int, listData: MutableLiveData<BasicListBean<GoodListBean>>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestGoodClassifyList(type, page, 10)?.subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<BasicListBean<GoodListBean>>(this) {
            override fun onSuccess(t: BasicListBean<GoodListBean>, json: String) {
                listData.value = t
            }

            override fun onFailed(message: String?) {
                listData.value = null
                super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }
        })
    }

    /**
     *购物车商品数量
     */
    fun reqShopCartNum(listData: MutableLiveData<ShopCartNumData>) {
        if(SpUtils.getInstance().isLogin) {
            MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqShopCartNum().subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<ShopCartNumData>(this) {
                override fun onSuccess(t: ShopCartNumData, json: String) {
                    listData.value = t
                }

                override fun onFailed(message: String?) {
                    listData.value = null
                    super.onFailed(message)
                }

                override fun onError(e: Throwable) {
                    listData.value = null
                    super.onError(e)
                }
            })
        }
    }

    /**
     * 地址列表
     */
    fun requestAddressList(listData: MutableLiveData<List<ReceiveAddressBean>>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestAddressList().subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<List<ReceiveAddressBean>>(this) {
            override fun onSuccess(t: List<ReceiveAddressBean>, json: String) {
                listData.value = t
            }

            override fun onFailed(message: String?) {
                listData.value = null
                super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }
        })
    }

    /**
     * 新增地址
     */
    fun requestNewAddAddressList(name: String, phone: String, areaFirst: String, areaSecond: String, areaThree: String, address: String, def: Int, listData: MutableLiveData<ReceiveAddressBean>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestNewAddAddressList(name, phone, areaFirst, areaSecond, areaThree, address, def).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<ReceiveAddressBean>(this) {
            override fun onSuccess(t: ReceiveAddressBean, json: String) {
                listData.value = t
            }

            override fun onFailed(message: String?) {
                listData.value = null
                super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }
        })
    }

    /**
     *  编辑地址
     */
    fun requestSaveEditAddressList(id: String, name: String, phone: String, areaFirst: String, areaSecond: String, areaThree: String, address: String, def: Int, listData: MutableLiveData<Any>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestSaveEditAddressList(id, name, phone, areaFirst, areaSecond, areaThree, address, def).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<Any>(this) {
            override fun onSuccess(t: Any, json: String) {
                listData.value = t
            }

            override fun onFailed(message: String?) {
                listData.value = null
                super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }
        })
    }

    /**
     * 删除地址
     */
    fun requestDeleteAddress(id: String, listData: MutableLiveData<Any>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestDeleteAddress(id).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<Any>(this) {
            override fun onSuccess(t: Any, json: String) {
                listData.value = t
            }

            override fun onFailed(message: String?) {
                listData.value = null
                super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }
        })
    }

    /**
     * 设置默认地址
     */
    fun requestSetDefAddress(id: String, listData: MutableLiveData<Any>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestSetDefAddress(id).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<Any>(this) {
            override fun onSuccess(t: Any, json: String) {
                listData.value = t
            }

            override fun onFailed(message: String?) {
                listData.value = null
                super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }
        })
    }

    //微信_电商订单_生成支付订单_v2
    fun createPayOrder4wx(listData: MutableLiveData<Long>, poids: String) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).createPayOrder4wx(poids).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<Long>(this) {
            override fun onSuccess(t: Long, json: String) {
                listData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
                listData.value = null
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }
        })
    }

    //微信_电商订单_生成支付订单_v2
    fun createPayOrder4zfb(listData: MutableLiveData<Long>, poids: String) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).createPayOrder4zfb(poids).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<Long>(this) {
            override fun onSuccess(t: Long, json: String) {
                listData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
                listData.value = null
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }
        })
    }

    //微信支付
    fun requestPay4WX(listData: MutableLiveData<WxPaySignBean>, oid: Long) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestPay4WX(oid).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<WxPaySignBean>(this) {
            override fun onSuccess(t: WxPaySignBean, json: String) {
                listData.value = t
            }

            override fun onFailed(message: String?) {
                listData.value = null
                super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }
        })
    }

    //支付宝支付
    fun requestPay4ZFB(listData: MutableLiveData<String>, oid: Long) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestPay4ZFB(oid).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<Any>(this) {
            override fun onSuccess(t: Any, json: String) {
                val httpResultBean = GsonUtils.parseJsonToBean(json, HttpResult::class.java)
                if (httpResultBean.isSuccess) {
                    listData.value = httpResultBean.msg
                }
            }

            override fun onFailed(message: String?) {
                listData.value = null
                super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }
        })
    }

    //查询订单是否支付成功
    fun requestPayIsSucc(liveData: MutableLiveData<PayResultBean>, oid: String) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqPayResultData(oid).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<PayResultBean>(this) {
            override fun onSuccess(t: PayResultBean, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                liveData.value = null
//                            super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                liveData.value = null
                super.onError(e)
            }
        })
    }

    /**从sku到确认下单页（订单预处理）
     * @author: merbng
     * @time: 2022/8/10 14:35
     * @description:
     */
    fun requestConfirmOrderDetail4GoodsDetail(listData: MutableLiveData<OrderDetailBean>, num: String, skuId: String) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestConfirmOrderDetail4GoodsDetail(num, skuId).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<OrderDetailBean>(this) {
            override fun onSuccess(t: OrderDetailBean, json: String) {
                if (t.checkNotNull()) listData.value = t
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }
        })
    }

    //订单详情
    fun requestOrderDetail(listData: MutableLiveData<OrderLoadDetailBean>, poId: String) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestOrderDetail(poId).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<OrderLoadDetailBean>(this) {
            override fun onSuccess(t: OrderLoadDetailBean, json: String) {
                if (t.checkNotNull()) listData.value = t
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }

            override fun onFailed(message: String?) {
                listData.value = null
                super.onFailed(message)
            }
        })
    }

    /**【确认订单-提交】从商品详情页购买构建的订单(持久化)
     * @author: merbng
     * @time: 2022/8/10 14:35
     * @description:
     */
    fun reqCommitOrder4GoodsDetail(listData: MutableLiveData<CommitOrderBean>, num: String, skuId: String, useScoreStatus: Int, userAddressId: Int, desc: String) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqCommitOrder4GoodsDetail(num, skuId, useScoreStatus, userAddressId, desc).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<CommitOrderBean>(this) {
            override fun onSuccess(t: CommitOrderBean, json: String) {
                if (t.checkNotNull()) listData.value = t
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }

            override fun onFailed(message: String?) {
                listData.value = null
                super.onFailed(message)
            }
        })
    }

    /**【确认订单-提交】从购物车购买构建的订单(持久化)
     * @author: merbng
     * @time: 2022/8/10 14:35
     * @description:
     */
    fun reqCommitOrder4Cart(listData: MutableLiveData<CommitOrderBean>, cartIds: String, useScoreStatus: Int, userAddressId: Int, descJson: String) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqCommitOrder4Cart(cartIds, useScoreStatus, userAddressId, descJson).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<CommitOrderBean>(this) {
            override fun onSuccess(t: CommitOrderBean, json: String) {
                if (t.checkNotNull()) listData.value = t
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }

            override fun onFailed(message: String?) {
                listData.value = null
                super.onFailed(message)
            }
        })
    }

    /**纯酒币（无现金）_电商订单_生成支付订单
     * @author: merbng
     * @time: 2022/8/17 19:45
     * @description:
     */
    fun reqCreateScoreOrder(listData: MutableLiveData<CreateScoreOrderBean>, poids: String) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestOnlyCoinPay(poids).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<CreateScoreOrderBean>(this) {
            override fun onSuccess(t: CreateScoreOrderBean, json: String) {
                if (t.checkNotNull()) listData.value = t
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }
        })
    }

    /**从购物车到确认下单页（订单预处理）
     * @author: merbng
     * @time: 2022/8/10 14:35
     * @description:
     */
    fun requestConfirmOrderDetail4Cart(listData: MutableLiveData<OrderDetailBean>, cartIds: String) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestConfirmOrderDetail4Cart(cartIds).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<OrderDetailBean>(this) {
            override fun onSuccess(t: OrderDetailBean, json: String) {
                if (t.checkNotNull()) listData.value = t
            }

            override fun onError(e: Throwable) {
                listData.value = null
                super.onError(e)
            }
        })
    }

    //客户端关闭未支付超时订单
    fun requestOverTimeNoPayCancel(liveData: MutableLiveData<Any>, poId: String) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestOverTimeNoPayCancel(poId).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<Any>(this) {
            override fun onSuccess(t: Any, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                liveData.value = null
                super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                liveData.value = null
                super.onError(e)
            }
        })
    }

    //用户取消订单
    fun reqCancelOrder(liveData: MutableLiveData<Any>, poId: String, cancellationId: Int, rollbackCart: Int) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqCancelOrder(poId, cancellationId, rollbackCart).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<Any>(this) {
            override fun onSuccess(t: Any, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                liveData.value = null
                super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                liveData.value = null
                super.onError(e)
            }
        })
    }

    //客户端关闭未支付超时订单
    fun reqCancelOrderReasonList(liveData: MutableLiveData<List<CancelReasonListBean>>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqCancelOrderReasonList().subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<List<CancelReasonListBean>>(this) {
            override fun onSuccess(t: List<CancelReasonListBean>, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                liveData.value = null
                super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                liveData.value = null
                super.onError(e)
            }
        })
    }

    //快递信息
    fun reqLogisticsInfo(poid: String, liveData: MutableLiveData<LogisticeInfoBean>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqLogisticsInfo(poid).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<LogisticeInfoBean>(this) {
            override fun onSuccess(t: LogisticeInfoBean, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                liveData.value = null
                super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                liveData.value = null
                super.onError(e)
            }
        })
    }

    //商品详情
    fun reqGoodsDetails(liveData: MutableLiveData<GoodsDetails>, spuId: Long, skuId: Long) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqGoodsDetails(spuId, skuId).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<GoodsDetails>(this) {
            override fun onSuccess(t: GoodsDetails, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                liveData.value = null
                super.onFailed(message)
            }
        })
    }

    //商品详情
    fun reqGoodsDetails(apiResponse: APIResponse<GoodsDetails>, spuId: Long, skuId: Long) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqGoodsDetails(spuId, skuId).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(apiResponse)
    }

    //相关商品推荐 type类型：0-后端分类id数组字符串，1-商品spuId数组字符串，2-商品订单id数组字符串,3-默认(不需要传ids)
    fun reqRecommendGoods(liveData: MutableLiveData<BasicListBean<GoodListBean>>, ids: String, type: Int, shopId: Int, page: Int, size: Int) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqGoodsDetailsRecommend(ids, type, shopId, page, size).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<BasicListBean<GoodListBean>>(this) {
            override fun onSuccess(t: BasicListBean<GoodListBean>, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
                liveData.value = null
            }
        })
    }

    //赠品详情
    fun reqGoodsDetailsGiftData(liveData: MutableLiveData<GoodsGiftBean>, skuId: Int, spuId: Int) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqGoodsDetailsGiftData(skuId, spuId).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<GoodsGiftBean>(this) {
            override fun onSuccess(t: GoodsGiftBean, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
                liveData.value = null
            }
        })
    }

    //种草秀
    fun reqGoodsDetailsGrassShow(liveData: MutableLiveData<BasicListBean<GrassShowBean>>, spuId: Long, page: Int, size: Int) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqGoodsDetailsGrassShow(spuId, page, size).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<BasicListBean<GrassShowBean>>(this) {
            override fun onSuccess(t: BasicListBean<GrassShowBean>, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
                liveData.value = null
            }
        })
    }

    //商品详情页评价
    fun reqGoodsEvaluation(liveData: MutableLiveData<BasicListBean<EvaluationBean>>, spuId: Long, page: Int, size: Int) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqGoodsDetailEvaluation(spuId, page, size).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<BasicListBean<EvaluationBean>>(this) {
            override fun onSuccess(t: BasicListBean<EvaluationBean>, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
                liveData.value = null
            }
        })
    }

    //我的订单列表
    fun reqMyOrderList(liveData: MutableLiveData<BasicListBean<MyOrderListBean>>, status: Int?, keyword: String, page: Int) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqMyOrderList(status, keyword, page, 20).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<BasicListBean<MyOrderListBean>>(this) {
            override fun onSuccess(t: BasicListBean<MyOrderListBean>, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
                liveData.value = null
            }
        })
    }

    //删除订单
    fun reqDeleteOrder(liveData: MutableLiveData<Boolean>, poId: String) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestDelOrder(poId).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<Boolean>(this) {
            override fun onSuccess(t: Boolean, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
                liveData.value = false
            }
        })
    }

    /**
     * 修改地址（订单详情）
     */
    fun reqChangeReciveAddress(poId: String, userAddressId: Int, liveData: MutableLiveData<Boolean>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqChangeReciveAddress(poId, userAddressId).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<Boolean>(this) {
            override fun onSuccess(t: Boolean, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
                liveData.value = false
            }
        })
    }


    /**
     * 设置选中地址
     */
    fun reqSetSelectAddress(userAddressId: Int, liveData: MutableLiveData<Boolean>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqSetSelectAddress(userAddressId).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<Boolean>(this) {
            override fun onSuccess(t: Boolean, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
                liveData.value = false
            }
        })
    }

    /**
     * 售后订单列表
     */
    fun reqAfterSaleList(page: Int, size: Int, liveData: MutableLiveData<BasicListBean<AfterSaleListBean>>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqAfterSaleList(page, size).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<BasicListBean<AfterSaleListBean>>(this) {
            override fun onSuccess(t: BasicListBean<AfterSaleListBean>, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
            }
        })
    }

    /**
     * 售后单详情
     */
    fun reqLoadAfterSale(afterSaleId: Long, liveData: MutableLiveData<AfterSalesInfoBean>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqLoadAfterSale(afterSaleId).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<AfterSalesInfoBean>(this) {
            override fun onSuccess(t: AfterSalesInfoBean, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
            }
        })
    }

    /**
     * 撤销售后
     */
    fun reqRevokeAfterSale(afterSaleId: Long, liveData: MutableLiveData<AfterSalesRevokeBean>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqRevokeAfterSale(afterSaleId).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<AfterSalesRevokeBean>(this) {
            override fun onSuccess(t: AfterSalesRevokeBean, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
            }
        })
    }

    /**
     * 申请售后
     */
    fun reqApplyAfterSale(json: String, liveData: MutableLiveData<AfterSalesInfoBean>) {
        val body = RequestBody.create("application/json; charset=utf-8".toMediaTypeOrNull(), json)
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqApplyAfterSale(body).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<AfterSalesInfoBean>(this) {
            override fun onSuccess(t: AfterSalesInfoBean, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
            }
        })
    }

    //确认收货
    fun requestConfirmRecive(liveData: MutableLiveData<ReceiveOrderBean>, poId: String) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestConfirmRecive(poId).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<ReceiveOrderBean>(this) {
            override fun onSuccess(t: ReceiveOrderBean, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                liveData.value = ReceiveOrderBean()
                super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                liveData.value = ReceiveOrderBean()
                super.onError(e)
            }
        })
    }

    //确认收货（预处理，判断是否有正在处理的售后单）
    fun reqPreConfirmReceive(liveData: MutableLiveData<Boolean>, poId: String) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqPreConfirmReceive(poId).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<Boolean>(this) {
                override fun onSuccess(t: Boolean, json: String) {
                    liveData.value = t
                }

                override fun onError(e: Throwable) {
                    liveData.value = null
                    super.onError(e)
                }
            })
        }
    }

    /**
     * 购物车
     */
    fun reqShopCartList(liveData: MutableLiveData<ArrayList<ShopCartBean>>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqShopCartList().subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<ArrayList<ShopCartBean>>(this) {
                override fun onSuccess(t: ArrayList<ShopCartBean>, json: String) {
                    liveData.value = t
                }

                override fun onFailed(message: String?) {
                    super.onFailed(message)
                    liveData.value = null

                }
            })
        }
    }

    /**
     * 购物车选中
     */
    fun reqSelectCartShop(cartIds: String, selected: Int, liveData: MutableLiveData<Boolean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqSelectCartShop(cartIds, selected).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<Any>(this) {
                override fun onSuccess(t: Any, json: String) {
                    liveData.value = true
                }

                override fun onFailed(message: String?) {
                    super.onFailed(message)
                    liveData.value = false
                }
            })
        }
    }

    /**
     * 删除购物车商品
     */
    fun reqDelCartShop(ids: String, liveData: MutableLiveData<AfterSalesRevokeBean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqDelCartShop(ids).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<AfterSalesRevokeBean>(this) {
                override fun onSuccess(t: AfterSalesRevokeBean, json: String) {
                    liveData.value = t
                }

                override fun onFailed(message: String?) {
                    super.onFailed(message)
                    liveData.value = AfterSalesRevokeBean()
                }
            })
        }
    }

    /**
     * 添加购物车商品
     */
    fun reqAddCartShop(autoMaxNum: Int = 0, skuid: Long, num: Int, liveData: MutableLiveData<ShopCartAddBean>) {
        SpUtils.getInstance().loadUserBean?.data?.token?.let {
            MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqAddCartShop(autoMaxNum, num, skuid).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<ShopCartAddBean>(this) {
                override fun onSuccess(t: ShopCartAddBean, json: String) {
                    liveData.value = t
                }

                override fun onFailed(message: String?) {
                    super.onFailed(message)
                    liveData.value = ShopCartAddBean()

                }

            })
        }
    }

    /**
     *  申请详情 判断是否申请粉丝
     */
    fun reqFansApplyDetail(liveData: MutableLiveData<Any>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqFansApplyDetail().subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<Any>(this) {
            override fun onSuccess(t: Any, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
                liveData.value = null
            }

            override fun onError(e: Throwable) {
                super.onError(e)
                liveData.value = null
            }
        })
    }


    //自动确认收货（客户端倒计时结束调用）
    fun requestOverTimeAutoReceive(liveData: MutableLiveData<OverTimeReceiveBean>, poId: String) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestOverTimeAutoReceive(poId).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<OverTimeReceiveBean>(this) {
            override fun onSuccess(t: OverTimeReceiveBean, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                liveData.value = null
                super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                liveData.value = null
                super.onError(e)
            }
        })
    }

    //我的商品评价列表
    fun requestMyEvaluationList(liveData: MutableLiveData<BasicListBean<EvaluationBean>>, type: Int, page: Int) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestMyEvaluationList(type, page, 10).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<BasicListBean<EvaluationBean>>(this) {
            override fun onSuccess(t: BasicListBean<EvaluationBean>, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                liveData.value = null
                super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                liveData.value = null
                super.onError(e)
            }
        })
    }

    //评价配置信息
    fun requestEvaluationConfig(liveData: MutableLiveData<EvaluationConfigBean>, productInfo: Int) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestEvaluationConfig(productInfo).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<EvaluationConfigBean>(this) {
            override fun onSuccess(t: EvaluationConfigBean, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                liveData.value = null
                super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                liveData.value = null
                super.onError(e)
            }
        })
    }

    //发布评价
    fun requestAddEvaluation(liveData: MutableLiveData<PublishEvaluationBean>, json: String) {
        val body = RequestBody.create("application/json; charset=utf-8".toMediaTypeOrNull(), json)
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestAddEvaluation(body).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<PublishEvaluationBean>(this) {
            override fun onSuccess(t: PublishEvaluationBean, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                liveData.value = null
                super.onFailed(message)
            }

            override fun onError(e: Throwable) {
                liveData.value = null
                super.onError(e)
            }
        })
    }

    //根据店铺id查询商品列表
    fun reqAllStoreGoods(liveData: MutableLiveData<BasicListBean<GoodListBean>>, shopId: Int, page: Int, size: Int) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).reqAllStoreGoods(shopId, page, size).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<BasicListBean<GoodListBean>>(this) {
            override fun onSuccess(t: BasicListBean<GoodListBean>, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
                liveData.value = null
            }
        })
    }

    //秒杀配置
    fun reqSeckillConfig(liveData: MutableLiveData<SeckillConfigBean>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestSeckillConfig().subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<SeckillConfigBean>(this) {
            override fun onSuccess(t: SeckillConfigBean, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
                liveData.value = null
            }
        })
    }

    //秒杀分类
    fun reqSeckillCategory(liveData: MutableLiveData<ArrayList<SeckillCategoryBean>>) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestSeckillCategory().subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<ArrayList<SeckillCategoryBean>>(this) {
            override fun onSuccess(t: ArrayList<SeckillCategoryBean>, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
                liveData.value = null
            }
        })
    }

    //秒杀分类商品
    fun reqSeckillCategoryActList(liveData: MutableLiveData<BasicListBean<SeckillCategoryActList>>,cid:Int,page: Int) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestSeckillCategoryActList(cid,page,20).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<BasicListBean<SeckillCategoryActList>>(this) {
            override fun onSuccess(t: BasicListBean<SeckillCategoryActList>, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
                liveData.value = null
            }
        })
    }
    //获取酒窖秒杀商品列表
    fun reqSeckillWineryActList(liveData: MutableLiveData<BasicListBean<SeckillCategoryActList>>,shopId:Int,page: Int) {
        MyHttpUtil.getInstance().getApi(NewRetailApi::class.java).requestSeckillWineryActList(shopId,page,10).subscribeOn(Schedulers.newThread())?.observeOn(AndroidSchedulers.mainThread())?.subscribe(object : APIResponse<BasicListBean<SeckillCategoryActList>>(this) {
            override fun onSuccess(t: BasicListBean<SeckillCategoryActList>, json: String) {
                liveData.value = t
            }

            override fun onFailed(message: String?) {
                super.onFailed(message)
                liveData.value = null
            }
        })
    }

}
